
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Model;
using log4net.Repository.Hierarchy;
using IGlobalist.Cms.Bll;
using System.IO;
using OneEducation.Common;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class HomeTree : BaseUserControl
    {
        protected string xmlPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/Admin/XML/HomeCategories.xml");
        public event EventHandler TreeNodeClick;
        public event EventHandler TreeLMSClick;
        public event EventHandler EvenLogMenu;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (EvenLogMenu != null)
                    EvenLogMenu(this, new EventArgs());                      
            }
            string target = Request.Form["__EVENTTARGET"];
            if (!string.IsNullOrEmpty(target))
            {
                if (target.Equals(this.tvCategoryEdu.UniqueID))
                {
                    if (ViewState["previousArg"] != null)
                    {
                        string previousArg = (string)ViewState["previousArg"];
                        if (previousArg.Equals(Request.Form["__EVENTARGUMENT"]))
                        {
                            try
                            {
                                if (TreeNodeClick != null && tvCategoryEdu.SelectedNode.Depth == 1)
                                {
                                    this.SelectedValue = tvCategoryEdu.SelectedValue;

                                    TreeNodeClick(this, e);
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
                }
            }

        }

        public string SelectedValue
        {
            get { return Convert.ToString(ViewState["_ClassID"]); }
            set { ViewState["_ClassID"] = value; }
        }

        private void PopulateBind(IContentCategory category, TreeNode node)
        {
            if (category.CategoryList != null)
                foreach (IContentCategory cat in category.CategoryList)
                {
                    TreeNode subNode = new TreeNode(cat.Name, cat.ID.ToString());
                    node.ChildNodes.Add(subNode);
                    PopulateBind(cat, subNode);
                }

        }

        public override void DataBind()
        {
            base.DataBind();
            
            tvCategoryEdu.Nodes.Clear();
            IContentCategory root = CategoryController.GetCategoryXMLList(true);
            TreeNode nodeRoot = new TreeNode("LMS categories ", "0");
            nodeRoot.NavigateUrl = "javascript:void(0)";
            PopulateBind(root, nodeRoot);
            tvCategoryEdu.Nodes.Add(nodeRoot);
            tvCategoryEdu.ExpandAll();
        }        
       
        public void BindRefesh(object sender, EventArgs e)
        {         
            //BindMedia();
        }

        public void BindCategory(object sender, EventArgs e)
        {
            DataBind();
            
        }

        protected void tvCategoryEdu_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (TreeLMSClick != null)
            {
                this.SelectedValue = tvCategoryEdu.SelectedValue;

                TreeLMSClick(this, e);
            }
            try
            {
                ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
            }
            catch (Exception ex)
            {
                Logger.Error("Unknown error", ex);
            }

        }
    }
}